package com.itheima.accountingdb.controller;

import com.itheima.accountingdb.pojo.Category;
import com.itheima.accountingdb.pojo.PageBean;
import com.itheima.accountingdb.pojo.Result;
import com.itheima.accountingdb.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author panweiwei
 */
@RestController
@RequestMapping("/categories")
@CrossOrigin
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /**
     * 添加分类
     *
     * @param category
     * @return
     */
    @PostMapping("/add")
    public Result<Category> add(@RequestBody Category category) {
        Category cg = categoryService.add(category);
        if (cg == null) {
            return Result.error("添加失败");
        }
        return Result.success(cg);
    }


    /**
     * 更新分类
     *
     * @param
     * @return
     */
    @PutMapping("/update/{id}")
    public Result update(@PathVariable long id, @RequestBody String name, String type) {
        int rows = categoryService.update(id, name, type);
        if (rows <= 0) {
            return Result.error("修改失败");
        }
        return Result.success();
    }


    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    private Result delete(@PathVariable Long id) {
        int rows = categoryService.delete(id);
        if (rows <= 0) {
            return Result.error("删除失败");
        }
        return Result.success();
    }


    /**
     * 查询分类
     * @param page
     * @param size
     * @param name
     * @param type
     * @param isDeleted
     * @param userId
     * @return
     */
    @GetMapping("/list")
    private PageBean<Category> list(Integer page, Integer size, String name, String type,Long isDeleted,Long userId) {
        System.out.println(page+" "+size+" "+name+" "+type+" "+isDeleted+" "+userId);
       PageBean<Category> pb = categoryService.list(page,size,name,type,isDeleted,userId);
       return pb;
    }
}
